<?php

namespace App\Http\Controllers\Mobile\Personal;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Personal\Interview;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Style;

class InterviewController extends CommonController{
    /**
     * 面试通知
     * @param Request $request
     * @return mixed
     */
    public function interview(Request $request){
        $user = auth()->guard('personal')->user();
        //关键词搜索
        $keyword  = trim($request->keyword);
        $interview = DB::table('interview as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_job as t3', 't1.job_id', '=', 't3.id')
            ->select(
                't1.interview_time','t1.id','t1.status','t1.com_id','t1.job_id',
                't2.company_name','t2.region','t2.contacts','t2.mobile','t2.phone',
                't3.job_title','t3.salary_above','t3.salary_below'
            )
            ->where('t1.personal_uid',$user->uid)
            ->where('t1.status','!=',4)
            ->where(function ($query) use($keyword) {
                if(strlen($keyword) > 0){
                    $query->where('t3.job_title', 'like', '%'.$keyword.'%')
                        ->orWhere('t2.company_name','like','%'.$keyword.'%');
                }
            })
            ->orderBy('updated_at', 'desc')
            ->paginate(10);
        //处理数组
        foreach($interview as $v){
            $v->company_name = str_limit($v->company_name,24);
            $v->job_title = str_limit($v->job_title,24);
            $v->region = str_limit(str_replace(',','',CollArea::getCityAddress($v->region)),18);
            $v->salary_above = ($v->salary_above) ? CollOption::getOptionName($v->salary_above,'expectedsalary') : '';
            $v->salary_below = ($v->salary_below) ? CollOption::getOptionName($v->salary_below,'expectedsalary') : '';
            $v->interview_time = substr($v->interview_time,0,16);
            $v->phone = ($v->mobile) ? $v->mobile : $v->phone;
        }
        //分页分配参数 过滤boolean假值
        $appends =arrayFilter(array(
            'keyword' => $keyword
        ));
        return Style::view('mobile.personal.interview',compact('interview','appends'));
    }

    /**
     * 面试删除
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function interviewDel(Request $request){
        $user = auth()->guard('personal')->user();
        $input = $request->only('id');
        $input['status'] = 4;
        if(!empty($request->id)){
            $result = Interview::where('personal_uid', $user->uid)->where('id', $request->id)->update($input);
            if($result){
                return response()->json(['status' => 200,'message'=>"删除面试记录成功"]);
            }else{
                return response()->json(['status' => 404,'message'=>"删除面试记录失败"]);
            }
        }else{
            return response()->json(['status' => 404,'message'=>"删除面试记录失败"]);
        }
    }
}